package org.hbgl.geoentity.admin.web.unit;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.hbgl.geoentity.admin.web.BaseController;
import org.hbgl.geoentity.core.util.ResponseUtil;
import org.hbgl.geoentity.db.domain.GeNode;
import org.hbgl.geoentity.db.domain.GeNodeApply;
import org.hbgl.geoentity.db.domain.GeUnit;
import org.hbgl.geoentity.db.domain.GeUser;
import org.hbgl.geoentity.db.util.EnumFile;
import org.hbgl.geoentity.db.vo.NodeApplyVo;
import org.hbgl.geoentity.db.vo.NodeVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

import static org.hbgl.geoentity.admin.util.ResponseCode.*;

/**
 * 代码功能：授权信息
 * 开发日期：2022/4/22
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
@RestController
@RequestMapping("/unit/node")
@Validated
public class UnitNodeController extends BaseController {

    /**
     * 获取节点申请列表
     */
    @GetMapping("/applylist")
    public Object appList(@RequestParam(defaultValue = "") String area,
                       @RequestParam(defaultValue = "-2") Integer state,
                       @RequestParam(defaultValue = "") String keywords,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        GeUser userCurrent = getCurrentUnitUser();
        List<NodeApplyVo> nodeApplyVoList = nodeService.selectNodeApplyList(userCurrent.getUnitId(), area, state, keywords, page, limit);
        return ResponseUtil.okList(nodeApplyVoList);
    }

    /**
     * 通过节点申请列表查询申请详情
     */
    @GetMapping("/nodeApplyDetail")
    public Object nodeApplyDetail(@NotNull Integer id) {
        NodeApplyVo nodeApplyVo = nodeService.findNodeApplyById(id);
        return ResponseUtil.ok(nodeApplyVo);
    }

    /**
     * 删除节点申请
     */
    @RequiresAuthentication
    @GetMapping("/nodeApplyDelete")
    public Object nodeApplyDelete(@NotNull Integer id) {
        NodeApplyVo nodeApplyVo = nodeService.findNodeApplyById(id);
        if(nodeApplyVo.getIfAdd()==EnumFile.IfAddState.Added) {
            return ResponseUtil.fail(USER_DELETE_NOT_ALLOWED, "该节点已添加不能被删除");
        }
        nodeService.delNodeApplyById(id);
        nodeService.delNodeByApplyId(id);
        return ResponseUtil.ok();
    }

    /**
     * 添加节点申请
     */
    @RequiresAuthentication
    @PostMapping("/applycreate")
    public Object create(@RequestBody GeNodeApply nodeApply) {
        GeUser userCurrent = getCurrentUnitUser();
        nodeApply.setUnitId(userCurrent.getUnitId());
        String nodeName = nodeApply.getNodeName();
        String nodeProfile = nodeApply.getNodeProfile();
        if (!ObjectUtils.allNotNull(nodeName, nodeProfile,
                userCurrent.getUnitId(), nodeApply.getContractAgreement(),
                nodeApply.getProvinceCode())) {
            return ResponseUtil.badArgument();
        }

        GeUnit unit = unitService.findById(userCurrent.getUnitId());
        if(!unit.getExamState().equals(EnumFile.ExamineState.Passed)) {
            return ResponseUtil.fail(AUTH_UNITNAME_EXIST, "单位审核通过后才能申请节点");
        }
        //查询节点名称是否存在
        List<GeNodeApply> nodeApplyByNodeName = nodeService.findNodeApplyByNodeName(nodeName);
        if(nodeApplyByNodeName != null && nodeApplyByNodeName.size()>0) {
            return ResponseUtil.fail(USER_NAME_EXIST, "节点名称已经存在");
        }
        List<GeNode> nodeByNodeName = nodeService.findNodeByNodeName(nodeName);
        if(nodeByNodeName != null && nodeByNodeName.size()>0) {
            return ResponseUtil.fail(USER_NAME_EXIST, "节点名称已经存在");
        }
//        List<GeNodeApply> nodeApplyByAddressCode = nodeService.findNodeApplyByAddressCode(nodeApply.getProvinceCode(), nodeApply.getCityCode(), nodeApply.getAreaCode());
//        if(null != nodeApplyByAddressCode && nodeApplyByAddressCode.size()>0) {
//            return ResponseUtil.fail(USER_NAME_EXIST, "该区域节点已经存在");
//        }
        nodeService.createNodeApply(nodeApply);
        return ResponseUtil.ok();
    }

    /**
     * 修改节点申请
     */
    @RequiresAuthentication
    @PostMapping("/applyupdate")
    public Object update(@RequestBody GeNodeApply nodeApply) {
        GeUser userCurrent = getCurrentUnitUser();
        nodeApply.setUnitId(userCurrent.getUnitId());
        Integer id = nodeApply.getId();
        String nodeName = nodeApply.getNodeName();
        String nodeProfile = nodeApply.getNodeProfile();
        if (!ObjectUtils.allNotNull(id)) {
            return ResponseUtil.badArgument();
        }
        //判断申请的节点是否可以修改
        NodeApplyVo nodeApplyById = nodeService.findNodeApplyById(id);
        // if (nodeApplyById.getState() == EnumFile.ExamineState.Passed) {
        //     return ResponseUtil.noUpdateDataExam();
        // }
        if (nodeApplyById.getIfAdd() == EnumFile.IfAddState.Added) {
            return ResponseUtil.noUpdateDataIfAdd();
        }
        if(nodeApplyById.getState() == EnumFile.ExamineState.One || nodeApplyById.getState() == EnumFile.ExamineState.Two) {
            return ResponseUtil.noUpdateDataExcem();
        }
        //查询节点名称是否存在
        List<GeNodeApply> nodeApplyByNodeName = nodeService.findNodeApplyByNodeName(nodeName);
        List<GeNodeApply> collect = nodeApplyByNodeName.stream().filter(p -> !p.getId().equals(id)).collect(Collectors.toList());
        if(collect != null && collect.size()>0) {
            return ResponseUtil.fail(USER_NAME_EXIST, "节点名称已经存在");
        }
        List<GeNode> nodeByNodeName = nodeService.findNodeByNodeName(nodeName);
        if(nodeByNodeName != null && nodeByNodeName.size()>0) {
            if(!nodeByNodeName.get(0).getApplyId().equals(nodeApply.getId())) {
                return ResponseUtil.fail(USER_NAME_EXIST, "节点名称已经存在");
            }
        }

//        List<GeNodeApply> nodeApplyByAddressCode = nodeService.findNodeApplyByAddressCode(nodeApply.getProvinceCode(), nodeApply.getCityCode(), nodeApply.getAreaCode());
//        nodeApplyByAddressCode = nodeApplyByAddressCode.stream().filter(p->!p.getId().equals(id)).collect(Collectors.toList());
//        if(null != nodeApplyByAddressCode && nodeApplyByAddressCode.size()>0) {
//            return ResponseUtil.fail(USER_NAME_EXIST, "该区域节点已经存在");
//        }

        nodeService.updateNodeApply(nodeApply);
        return ResponseUtil.ok();
    }

    /**
     * 获取节点列表
     */
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "-1") Integer nodeType,
                       @RequestParam(defaultValue = "-1") Integer state,
                       @RequestParam(defaultValue = "") String keywords,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        GeUser userCurrent = getCurrentUnitUser();
        List<NodeVo> nodeVoList = nodeService.selectUnitNodeList(userCurrent.getUnitId(), nodeType, state, keywords, page, limit);
        return ResponseUtil.okList(nodeVoList);
    }

    /**
     * 获取节点详情
     */
    @GetMapping("/nodedetail")
    public Object nodeDetail(@NotNull Integer nodeId) {
        GeUser userCurrent = getCurrentUnitUser();
        NodeVo nodeVo = nodeService.findNodeById(nodeId);
        return ResponseUtil.ok(nodeVo);
    }

}
